package com.liangzhong.modules.guide.mediator
{
	import com.common.queueloader.QueueLoader;
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.queueloader.QueueVo;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.greensock.TweenLite;
	import com.liangzhong.constants.ApplicationConstants;
	import com.liangzhong.constants.NotificationConstants;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.GuideEvent;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.events.TaskEvent;
	import com.liangzhong.mediator.ModuleMediator;
	import com.liangzhong.model.AppData;
	import com.liangzhong.modules.PlayerCenter.mediator.PlayerCenterMediator;
	import com.liangzhong.modules.court.CourtMediator.CourtMediator;
	import com.liangzhong.modules.leagueMatch.mediator.LeagueMatchMediator;
	import com.liangzhong.proxyconstant.club.ClubProxyConstant;
	import com.lz.football.module.guide.IGuide;
	
	import common.lvAward.ILvAwardView;
	
	import flash.display.DisplayObject;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import lz.controls.Alert;
	import lz.controls.managers.AnimationManager;
	import lz.controls.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.INotification;

	/**
	 *引导 mediator 
	 * @author lake
	 * 
	 */	
	public class GuideMediator extends ModuleMediator
	{
		public static const MediatorName :String = "GuideMediator";
		/**
		 *当前引导 
		 */		
		private var curGuide :IGuide;
		/**
		 *当前引导类型 全路径 
		 */		
		private var curGuideClassRef :String = "";
		/**
		 * 俱乐部等级
		 */		
		private var clubLv :int
		/**
		 *跳过引导按钮 
		 */		
		private var skinGuideBtn :SimpleButton;
		
		public function GuideMediator(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
		}
		/**
		 *舞台大小改变 
		 * @param e
		 * 
		 */		
		private function onResizeHandler(e :Event):void
		{
			var guideStep :int = AppData.getInstance().clubDatamanager.guideStep;
			if(guideStep <= 11)
			{
				if(skinGuideBtn)
				{
					GlobalDef.GameStage.addChild(skinGuideBtn);
					skinGuideBtn.x = GlobalDef.GameStage.stageWidth - skinGuideBtn.width - 15;
					skinGuideBtn.y = GlobalDef.GameStage.stageHeight - 115;
				}
			}
			else
			{
				if(skinGuideBtn && skinGuideBtn.parent)
				{
					skinGuideBtn.parent.removeChild(skinGuideBtn);
				}
			}
		}
		/**
		 * 
		 * 添加事件
		 */		
		override public function addEventListeners():void
		{
			AppEventDispatcher.getInstance().addEventListener(GuideEvent.POST_GUIDE,postGuideHandler);
			AppEventDispatcher.getInstance().addEventListener(GuideEvent.POST_GUIDE + "_refresh",refreshGuideHandler);
			AppEventDispatcher.getInstance().addEventListener(NotifierEvent.CLUB_LV_CHANGE,onClubLevHandler,false,-1,false);
			AppData.getInstance().taskManager.addEventListener(TaskEvent.GET_TASK_LIST,getTaskList);
			AppEventDispatcher.getInstance().addEventListener(TaskEvent.GET_TASK_FINISH_WARD,finishTaskH);
		}
		/**
		 * 
		 * 删除事件
		 */		
		override public function removeEventListeners():void
		{
			AppEventDispatcher.getInstance().removeEventListener(GuideEvent.POST_GUIDE,postGuideHandler);
			AppEventDispatcher.getInstance().removeEventListener(GuideEvent.POST_GUIDE + "_refresh",refreshGuideHandler);
			AppEventDispatcher.getInstance().removeEventListener(NotifierEvent.CLUB_LV_CHANGE,onClubLevHandler);
			AppData.getInstance().taskManager.removeEventListener(TaskEvent.GET_TASK_LIST,getTaskList);
			AppEventDispatcher.getInstance().removeEventListener(TaskEvent.GET_TASK_FINISH_WARD,finishTaskH);
			GlobalDef.GameStage.removeEventListener(Event.RESIZE,onResizeHandler);
		}
		/**
		 * 俱乐部升级
		 * @param e
		 * 
		 */		
		private function onClubLevHandler(e :Event):void
		{
			clubLv = AppData.getInstance().clubDatamanager.clubInfo.level;
			var guideStep :int = AppData.getInstance().clubDatamanager.guideStep;
			if((clubLv > 6 || curGuideClassRef == "com.lz.football.module.guide.view.MarkGuideView"))
			{
				TweenLite.delayedCall(.2,detectAction);
			}
		}
		/**
		 *延迟一些东西 
		 */		
		private function delaySomething():void
		{
			this.sendNotification(NotificationConstants.PLAY_EFFECT);
		}
		/**
		 * 首次获取任务列表 
		 * @param e
		 * 
		 */		
		private function getTaskList(e :Event):void
		{
			var finishTaskList :Array = AppData.getInstance().taskManager.getFinishTaskList();
			if(finishTaskList.length > 0)
			{
				this.sendNotification(NotificationConstants.APPEAR_FINISH_TASK,finishTaskList);
			}
		}
		/**
		 * 完成任务 
		 * @param e
		 * 
		 */		
		private function finishTaskH(e : TaskEvent):void
		{
			var tid :int = int(e.param.id);
		}
		/**
		 *提交引导 
		 * @param e
		 * 
		 */		
		private function postGuideHandler(e :GuideEvent):void
		{
			this.sendNotification(ApplicationConstants.GUIDE_POSTGUIDE,e.param);
			var step :int = int(e.param.step);
			AppData.getInstance().clubDatamanager.guideStep = step;
			if(step >= 11)
			{
				if(skinGuideBtn && skinGuideBtn.parent)
				{
					skinGuideBtn.parent.removeChild(skinGuideBtn);
				}
			}
		}
		/**
		 * 刷新引导 
		 * @param e
		 * 
		 */		
		private function refreshGuideHandler(e :GuideEvent):void
		{
			var step :int = int(e.param.step);
			if(step == 2 || step==5 || step==6 || step==11)
			{
				startGuide();
			}
			if(step == 3 || step==4)
			{
				detectAction();
			}
			if(step == 3 || step == 4)
			{
				this.sendNotification(NotificationConstants.PLAY_EFFECT);
			}
			if(step == 7 || step==11)
			{
				this.facade.removeMediator(LeagueMatchMediator.MediatorName);
			}
		}
		/**
		 * 跳过引导 
		 * @param e
		 * 
		 */		
		private function skinGuideH(e :MouseEvent):void
		{
			var info :String = "跳过新手引导将可以自由操作!\n 是否确认跳过？";
			Alert.show(info,"",Alert.YES | Alert.NO,GlobalDef.GameStage,alertH);
		}
		/**
		 *跳过引导确认 
		 * @param type
		 * 
		 */		
		private function alertH(type :int):void
		{
			if(type == Alert.YES)
			{
				removeEventListeners();
				var step :int = 31;
				AppData.getInstance().clubDatamanager.guideStep = step;
				this.sendNotification(ApplicationConstants.GUIDE_POSTGUIDE,{step:step});
				if(curGuide)
				{
					curGuide.clear();
				}
				if(skinGuideBtn && skinGuideBtn.parent)
				{
					skinGuideBtn.parent.removeChild(skinGuideBtn);
				}
				AppEventDispatcher.getInstance().dispatchEvent(new Event(NotifierEvent.BASE_INFO_CHANGE));
				var lev :int = AppData.getInstance().clubDatamanager.clubInfo.level;
				if(lev < 5)
				{
					var lvAwardView :ILvAwardView = Reflection.createInstance("common.lvAward.LvAwardView");
					lvAwardView.initUI();
					lvAwardView.showView(0);
					PopUpManager.addPopUp(lvAwardView as DisplayObject,GlobalDef.AlertContainer,GlobalDef.StageWidth,GlobalDef.StageHeight,true);
				}
			}
		}
		/**
		 * 监听消息列表
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [ClubProxyConstant.GET_CLUB_INFO,
				NotificationConstants.MATCH_SERVER_END].concat(super.listNotificationInterests());
		}
		/**
		 * 捕获信息
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification:INotification):void
		{
			var lev :int = AppData.getInstance().clubDatamanager.clubInfo.level;
			var guideStep :int = AppData.getInstance().clubDatamanager.guideStep;
			switch(notification.getName())
			{
				case ClubProxyConstant.GET_CLUB_INFO://获取俱乐部
				{
					var loader12 :QueueLoader = new QueueLoader();
					loader12.addEventListener(QueueLoaderEvent.QUEUE_COMPLETE,loadCompleteHandler);
					loader12.addEventListener(QueueLoaderEvent.ITEM_COMPLETE,onLItemComplete);
					if(guideStep < 21)
					{
						skinGuideBtn = Reflection.createInstance("com.com.skin.SkipGuideBtn");
						if(skinGuideBtn)
						{
							GlobalDef.GameStage.addEventListener(Event.RESIZE,onResizeHandler);
							skinGuideBtn.addEventListener(MouseEvent.CLICK,skinGuideH);
						}
						loader12.addQueue(new QueueVo("guide_res"),"res/swf/guide/guide_res_01.swf?" + GlobalDef.ReleaseVersion + ".swf");
					}
					if(lev < 15)
					{
						loader12.addQueue(new QueueVo("guide_lv_aw"),"res/swf/guide/guide_lv_aw.swf?" + GlobalDef.ReleaseVersion + ".swf");
					}
					if(guideStep == 0)
					{
						TweenLite.delayedCall(.3,detectAction);
					}
					loader12.queueLoadStart();
					break;
				}
				case NotificationConstants.MATCH_START://比赛开始
				{
					if(lev == 4 && guideStep==8)
					{
						processGuide("com.lz.football.module.guide.view.LongPassGuideView");
					}
					break;
				}
				case NotificationConstants.MATCH_END://比赛结束
				{
					if(clubLv == 2)
					{
						this.facade.removeMediator(CourtMediator.MediatorName);
						this.facade.removeMediator(LeagueMatchMediator.MediatorName);
					}
					detectAction();
					break;
				}
				case NotificationConstants.MATCH_SERVER_END://server比赛结束
				{
					//TweenLite.delayedCall(.1,detectAction);
					break;
				}
			}
		}
		/**
		 * item complete
		 * @param e
		 * 
		 */		
		private function onLItemComplete(e:QueueLoaderEvent):void
		{
			if(e.queueVo == null)
			{
				return;
			}
			switch(e.queueVo.queueName)
			{
				case "guide_res"://引导资源
				{
					var guideStep :int = AppData.getInstance().clubDatamanager.guideStep;
					if(guideStep > 0)
					{
						TweenLite.delayedCall(.3,detectAction);
					}
					break;
				}
			}
		}
		/**
		 * 加载完毕
		 */		
		private function loadCompleteHandler(e :Event):void
		{
			e.target.removeEventListener(QueueLoaderEvent.QUEUE_COMPLETE,loadCompleteHandler);
		}
		/**
		 *检测动作 
		 * 
		 */		
		private function detectAction():void
		{
			clubLv = AppData.getInstance().clubDatamanager.clubInfo.level;
			GlobalDef.GameStage.addEventListener(Event.ENTER_FRAME,enterFrameDetect);
		}
		/**
		 *每帧检测 
		 */		
		private function enterFrameDetect(e:Event):void
		{
			if(AnimationManager.getLength() == 0)
			{
				startGuide();
				GlobalDef.GameStage.removeEventListener(Event.ENTER_FRAME,enterFrameDetect);
			}
		}
		/**
		 *执行引导 
		 * @param classRef
		 * 
		 */		
		private function processGuide(classRef :String):void
		{
			if(curGuide)
			{
				curGuide.clear();
			}
			curGuideClassRef = classRef;
			curGuide = Reflection.createInstance(classRef);
			if(curGuide)
			{
				curGuide.process();
			}
			var guideStep :int = AppData.getInstance().clubDatamanager.guideStep;
			if(guideStep <= 11)
			{
				if(skinGuideBtn)
				{
					GlobalDef.GameStage.addChild(skinGuideBtn);
					skinGuideBtn.x = GlobalDef.GameStage.stageWidth - skinGuideBtn.width - 15;
					skinGuideBtn.y = GlobalDef.GameStage.stageHeight - 115;
				}
			}
			else
			{
				if(skinGuideBtn && skinGuideBtn.parent)
				{
					skinGuideBtn.parent.removeChild(skinGuideBtn);
				}
			}
		}
		/**
		 *引导开始 
		 */		
		private function startGuide():void
		{
			var lev :int = AppData.getInstance().clubDatamanager.clubInfo.level;
			var guideStep :int = AppData.getInstance().clubDatamanager.guideStep;
			if(guideStep >= 30)
			{
				return;
			}
			if(guideStep <= 11)
			{
				if(skinGuideBtn)
				{
					GlobalDef.GameStage.addChild(skinGuideBtn);
					skinGuideBtn.x = GlobalDef.GameStage.stageWidth - skinGuideBtn.width - 15;
					skinGuideBtn.y = GlobalDef.GameStage.stageHeight - 115;
				}
			}
			else
			{
				if(skinGuideBtn && skinGuideBtn.parent)
				{
					skinGuideBtn.parent.removeChild(skinGuideBtn);
				}
			}
			if(guideStep < 8)
			{
				processGuide("com.lz.football.module.guide.view.MarkGuideView");
			}
			if(lev==1 && guideStep==0)
			{
				processGuide("com.lz.football.module.guide.view.FirstMatchGuideView");
			}
			else if(lev == 2 && guideStep==1)
			{
				processGuide("com.lz.football.module.guide.view.LoginAwardGuideView");
			}
			else if(lev == 2 && guideStep==2)
			{
				processGuide("com.lz.football.module.guide.view.PlayerSearchGuideView");
			}
			else if(lev == 2 && guideStep==3)
			{
				processGuide("com.lz.football.module.guide.view.UsePlayerCardGuideView");
			}
			else if(lev == 3 && guideStep==4)
			{
				processGuide("com.lz.football.module.guide.view.LineupSetGuideView");
			}
			else if(lev == 3 && guideStep==5)
			{
				processGuide("com.lz.football.module.guide.view.RoleFreshGuideView");
			}
			else if(lev == 3 && guideStep==6)
			{
				processGuide("com.lz.football.module.guide.view.TaskGuideView");
			}
			else if(lev == 4 && guideStep==7)
			{
				processGuide("com.lz.football.module.guide.view.SignGuideView");
			}
			else if(lev == 5 && guideStep==9)
			{
				processGuide("com.lz.football.module.guide.view.VipGuideView");
			}
			else if(lev == 6 && guideStep==10)
			{
				processGuide("com.lz.football.module.guide.view.ShopGuideView");
			}
			else if(lev == 6 && guideStep==11)
			{
				this.facade.removeMediator(PlayerCenterMediator.MediatorName);
				processGuide("com.lz.football.module.guide.view.UsePhysicalGuideView");
			}
			else if(lev == 7 && guideStep==12)
			{
				processGuide("com.lz.football.module.guide.view.EquipGuideView");
			}
			else if(lev == 8 && guideStep==13)
			{
				processGuide("com.lz.football.module.guide.view.LineupSettingGuideView");
			}
			else if(lev == 9 && guideStep==14)
			{
				processGuide("com.lz.football.module.guide.view.GradeGuideView");
			}
			else if(lev == 10 && guideStep==15)
			{
				processGuide("com.lz.football.module.guide.view.LadderGuideView");
			}
			else if(lev == 11 && guideStep==16)
			{
				processGuide("com.lz.football.module.guide.view.MailGuideView");
			}
			else if(lev == 13 && guideStep==17)
			{
				processGuide("com.lz.football.module.guide.view.FriendGuideView");
			}
			else if(lev == 15 && guideStep==18)
			{
				processGuide("com.lz.football.module.guide.view.CountryGuideView");
			}
			else if(lev == 20 && guideStep==19)
			{
				processGuide("com.lz.football.module.guide.view.LivelyGuideView");
			}
			else
			{
				if(guideStep == 4 && lev == 2)
				{
					if(curGuide)
					{
						curGuide.clear();
					}
					var param :Object = {step:31};
					this.sendNotification(ApplicationConstants.GUIDE_POSTGUIDE,param);
					var step :int = int(param.step);
					AppData.getInstance().clubDatamanager.guideStep = step;
					startGuide();
				}
			}
			//测试
			//processGuide("com.lz.football.module.guide.view.UsePlayerCardGuideView");
		}
		/**
		 * 注册 
		 * 
		 */		
		override public function onRegister():void
		{
			addEventListeners();
		}
		
		override public function removeMediator():void
		{
			facade.removeMediator(MediatorName);
		}
		
		override public function onRemove():void
		{
			super.onRemove();
			removeEventListeners();
		}
	}
}